package com.swind.fast.common.handler;

import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authc.AuthenticationException;
import com.swind.fast.common.api.vo.Result;

import java.io.IOException;
import lombok.extern.slf4j.Slf4j;

// @ControllerAdvice
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    // @ExceptionHandler(value = AuthenticationException.class)
    public Result<?>  handleException(HttpServletResponse response, Exception exception) {
        // response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        // response.setContentType("application/json");
        // try {
        //     response.getWriter().write("{\"error\": \"" + exception.getMessage() + "\"}");
        // } catch (IOException e) {
        //     e.printStackTrace();
        // }
        log.info("{\"error\": \"" + exception.getMessage() + "\"}");
        exception.printStackTrace();

        return Result.error(exception.getMessage());
    }
}
